package lib.pacificapp.widget;

import java.util.ArrayList;
import java.util.List;

import lib.pacificapp.framework.R;

import com.squareup.picasso.Picasso;
  
 

 
 
 

import android.content.Context;
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet; 
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType; 
 
public class Img   extends LinearLayout{
	 

	 private static List<Bitmap> imgfactory;
	    
	    private static List<Bitmap> getImgfactory(){
	    	if(imgfactory==null)
	    		imgfactory=new ArrayList<Bitmap>();
	    	return imgfactory;
	    }
	  private Context  mContext; 
	    public Img(Context context, AttributeSet attrs) {
			super(context, attrs);
			mContext=context;
			InitView(mContext);
		    }

	    public Img(Context context) {
			super(context);
			mContext=context; 
			InitView(mContext);
		    }

	 
	     int height=0;
	     int width=0;
	     int radius=0;
	     public void setImageBitmap(Bitmap bip){
	    	   image.setImageBitmap(bip);
	    	    removeView(t);
	     }
	     protected void setSize(int iwidth,int iheight){
	    	height=iheight;
	    	width=iwidth;
	    	LayoutParams ps=new LayoutParams(height,width);
	     	image.setLayoutParams(ps); 
	    }
	    
	   
	     private int circle=0;
	     /***
	   *  */
	  private  ImageView image;
	  private TextView t;
	  private void InitView(Context context){
		  if(image==null){
			  
		     image=new  ImageView(context);
		     image.setVisibility(View.VISIBLE); 
	    	LayoutParams pars=new LayoutParams(-1,-1);
	    	    image.setLayoutParams(pars); 
		         image.setAdjustViewBounds(true);
		     image.setScaleType(ScaleType.FIT_XY);
	    	   
	     
	       t=new TextView(context);
	       t.setText("");
	       this.addView(t);
	       this.addView(image);
	       
	       } 
	  }
	  
    
	  //	private DisplayImageOptions ;
	  public void url(String url){  
		    if(url.equals("")){
		    	image.setImageResource(R.drawable.lib_img_default);
			   return;
		    }
		    Picasso.with(getContext()).load(url).placeholder(R.drawable.lib_img_default).error(R.drawable.lib_img_default).fit().into(image);
		  
     }
	  public void url(String url,int width,int height){  
		  if(url.equals("")){
				image.setImageResource(R.drawable.lib_img_default);
			    return;
		  }
		  Picasso.with(getContext()).load(url).placeholder(R.drawable.lib_img_default).error(R.drawable.lib_img_default).resize(width,height).centerCrop().into(image);
		 //PaImageLoaderFactory.getImageLoader().displayImage(url, image);
     }
	  public void url(String url,int width,int height,int error){  
		  if(url.equals("")){
				image.setImageResource(error);
			    return;
		  }
		  Picasso.with(getContext()).load(url).placeholder(error).error(error).resize(width,height).centerCrop().into(image);
		 //PaImageLoaderFactory.getImageLoader().displayImage(url, image);
     }
	  /**释放内存*/
     public void clean(){
	  recycleImageViewBitMap(image);
	   
      }
	  private static void recycleImageViewBitMap(ImageView imageView) {
		    if (imageView != null) {
		        BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
		        rceycleBitmapDrawable(bd);
		    }
		}

		private static void rceycleBitmapDrawable(BitmapDrawable bitmapDrawable) {
		    if (bitmapDrawable != null) {
		        Bitmap bitmap = bitmapDrawable.getBitmap();
		        rceycleBitmap(bitmap);
		    }
		    bitmapDrawable = null;
		}

		private static void rceycleBitmap(Bitmap bitmap) {
		    if (bitmap != null && !bitmap.isRecycled()) {
		        bitmap.recycle();
		        bitmap = null;
		    }
		}
 
	  
	 
     
 }

	   
